<?php
/*
	新版系统支付接口
	新版支付接口的微信支付、支付宝支付等有异步结果回调通知的支付方式，此处支付完成后不再更改支付状态，待收到异步支付结果通知后再修改支付状态。
	新增付款码支付方式，付款码支付获取订单支付进行，进入支付页面，发起支付请求，完成支付，提示支付成功！然后呢？


*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'pay';
$return=array();
$setting=uni_setting('pay');
set_html('title','支付页面');
if($_RQ['op']=='copy'){
    if($_SESSION['container']=='wechat'){
        $id=intval($_RQ['id']);
        $pay_url=custom_url('core/member.pay/copy/id='.$id,true);
        $return['pay_url']=$pay_url;
        exi($return);
    }else{
        $_RQ['op']='pay';
    }
}
if($_RQ['op']=='pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('发起支付失败！请先选择要支付的订单！','error');
    }
    $pay=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    if(!$pay){
        exi('找不到要支付的订单！','error');
    }elseif($pay['no_type']){
        $pay['no_type']=json_decode($pay['no_type'],true);
    }
    if($pay['status']){
        exi('订单已支付！','error');
    }
	if($pay['jianfee']>0){
        $pay['fee']=$pay['fee']-$pay['jianfee'];
    }
    $return=array(
        'id'=>$pay['id'],
        'tid'=>$pay['tid'],
        'fee'=>$pay['fee'],
		'jianfee'=>$pay['jianfee'],
    );
    $pay_types=array('hdfk','yinlian','alipay','wechat','diypay','diypay2','go2pay','allinpay','hnapay');
    if($_SESSION['uid']){
        $pay_types[]='credit';
    }
	$setting['allinpay']=0;
	if($pay['merchid']){
		$jy_merch=cfc('core')->plugin_exists('jy_merch');
		$jy_merch=plugin('jy_merch')->plugin_setting('setting');
		if($jy_merch['isone_allinpay']){
			$merchset=pdo_getcolumn('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$pay['merchid']),'setting');
			$merchset=json_decode($merchset,true);
			$setting['allinpay_showtype']=$merchset['allinpay_showtype'];
			$setting['allinpay_pay_types']=$merchset['allinpay_pay_types'];
		}
	}
    /*
    if($_SESSION['container']!='wechat'){
        //不在微信环境，添加支付宝支付
        $pay_types[]='alipay';
    }
    if($_SESSION['container']!='alipay'){
        //不在支付宝环境，添加微信支付
        $pay_types[]='wechat';
    }*/
	//通联支付
    if($setting['allinpay_showtype']){
        $allinpay_pay_types=str_array($setting['allinpay_pay_types']);
        if($pay['no_type']){
            $allinpay_pay_types=array_diff($allinpay_pay_types,$pay['no_type']);
        }
    }
    //聚合支付
    if($setting['adapay_showtype']){
        $adapay_pay_types=str_array($setting['adapay_pay_types']);
        if($pay['no_type']){
            $adapay_pay_types=array_diff($adapay_pay_types,$pay['no_type']);
        }
    }
    //杉德支付
    if($setting['sandpay_showtype']){
        $sandpay_pay_types=str_array($setting['sandpay_pay_types']);
        if($pay['no_type']){
            $sandpay_pay_types=array_diff($sandpay_pay_types,$pay['no_type']);
        }
    }
	//汇付宝支付
    if($setting['heepay_showtype']){
        $heepay_pay_types=array('fast_pay');
        if($pay['no_type']){
            $heepay_pay_types=array_diff($heepay_pay_types,$pay['no_type']);
        }
    }
	//汇付天下支付
    if($setting['huifuapi_showtype']){
        $huifu_pay_types=array('wechat');
        if($pay['no_type']){
            $huifu_pay_types=array_diff($huifu_pay_types,$pay['no_type']);
        }
    }
    if($pay['no_type']){
        $pay_types=array_diff($pay_types,$pay['no_type']);
    }
	if($setting['allinpay_showtype']!=3){
        foreach($pay_types as $key=>$type){
            if($setting[$type]!=1 && $type!='union_wap' && $type!='union_online' && $type!='fast_pay'){
                unset($pay_types[$key]);
            }else{
                $return['is'.$type]=1;
            }
            if($type=='credit'){
                $iscancredit=1;
            }
        }
        if($allinpay_pay_types){
            foreach($allinpay_pay_types as $key=>$type){
				if($_SESSION['container']!='wechat' || $type!='alipay'){
					if(!$return['is'.$type]){
						$pay_types[]=$type;
						$return['is'.$type]=1;
					}
				}
            }
        }
    }else{
        $pay_types=$allinpay_pay_types;
        foreach($allinpay_pay_types as $key=>$type){
            $return['is'.$type]=1;
        }
    }
    if($setting['adapay_showtype']!=3){
        foreach($pay_types as $key=>$type){
            if($setting[$type]!=1 && $type!='union_wap' && $type!='union_online' && $type!='fast_pay'){
                unset($pay_types[$key]);
            }else{
                $return['is'.$type]=1;
            }
            if($type=='credit'){
                $iscancredit=1;
            }
        }
        if($adapay_pay_types){
            foreach($adapay_pay_types as $key=>$type){
				if($_SESSION['container']!='wechat' || $type!='alipay'){
					if(!$return['is'.$type]){
						$pay_types[]=$type;
						$return['is'.$type]=1;
					}
				}
            }
        }
    }else{
        $pay_types=$adapay_pay_types;
        foreach($adapay_pay_types as $key=>$type){
            $return['is'.$type]=1;
        }
    }
    if($setting['sandpay_showtype']!=3){
        foreach($pay_types as $key=>$type){
            if($setting[$type]!=1 && $type!='union_wap' && $type!='union_online' && $type!='fast_pay'){
                unset($pay_types[$key]);
            }else{
                $return['is'.$type]=1;
            }
            if($type=='credit'){
                $iscancredit=1;
            }
        }
        if($sandpay_pay_types){
            foreach($sandpay_pay_types as $key=>$type){
				if($_SESSION['container']!='wechat' || $type!='alipay'){
					if(!$return['is'.$type]){
						$pay_types[]=$type;
						$return['is'.$type]=1;
					}
				}
            }
        }
    }else{
        $pay_types=$sandpay_pay_types;
        foreach($sandpay_pay_types as $key=>$type){
            $return['is'.$type]=1;
        }
    }
	if($setting['heepay_showtype']!=3){
        foreach($pay_types as $key=>$type){
            if($setting[$type]!=1 && $type!='union_wap' && $type!='union_online' && $type!='fast_pay'){
                unset($pay_types[$key]);
            }else{
                $return['is'.$type]=1;
            }
            if($type=='credit'){
                $iscancredit=1;
            }
        }
        if($heepay_pay_types){
            foreach($heepay_pay_types as $key=>$type){
                if(!$return['is'.$type]){
                    $pay_types[]=$type;
                    $return['is'.$type]=1;
                }
            }
        }
    }else{
        $pay_types=$heepay_pay_types;
        foreach($heepay_pay_types as $key=>$type){
            $return['is'.$type]=1;
        }
    }
	if($setting['huifuapi_showtype']!=3){
        foreach($pay_types as $key=>$type){
            if($setting[$type]!=1 && $type!='union_wap' && $type!='union_online' && $type!='fast_pay'){
                unset($pay_types[$key]);
            }else{
                $return['is'.$type]=1;
            }
            if($type=='credit'){
                $iscancredit=1;
            }
        }
        if($huifu_pay_types){
            foreach($huifu_pay_types as $key=>$type){
                if(!$return['is'.$type]){
                    $pay_types[]=$type;
                    $return['is'.$type]=1;
                }
            }
        }
    }else{
        $pay_types=$huifu_pay_types;
        foreach($huifu_pay_types as $key=>$type){
            $return['is'.$type]=1;
        }
    }
    if($_RQ['pay_type']=='alipay'){
        $pay_types=array('alipay');
    }
    //小程序环境下注销支付宝支付
    if($_SESSION['wxapp_id'] && $_SESSION['fromh5wxapp']){
        unset($return['isalipay']);
        unset($return['isalipay_url']);
    }
	if($_SESSION['container']=='wechat' && $setting['alipay_wechat']){
		unset($return['isalipay']);
        unset($return['isalipay_url']);
	}
    $return['pay_types']=array_values($pay_types);
    if($_SESSION['uid'] && $iscancredit && $setting['adapay_showtype']!=3 && $setting['sandpay_showtype']!=3){
        $return['mid']=$_SESSION['uid'];
        if($pay['merchid']){
            $member=pdo_get('jy_merch_member',array('merchid'=>$pay['merchid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchid','credit1','credit2','credit3','credit4','credit5','credit6'));
        }
        if(!$member['merchid']){
            $member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','credit3','credit4','credit5','credit6'));
        }
        $return['credit']=$member['credit2'];
        //插件虚拟设置
        if($pay['plugin_xuni']){
            $creditdata=json_decode($pay['plugin_xuni'],true);
        }else{
            if(!$pay['merchid']){
                $creditdata=model('core/member')->creditdata($member);
            }else{
                $creditdata=model('jy_merch/member')->creditdata($member);
            }
        }
        foreach($creditdata as $k=>&$cv){
            //剔除余额支付
            if($cv['type']==2){
                unset($creditdata[$k]);
            }
            if($pay['no_type']){
                foreach($pay['no_type'] as $nov){
                    if($nov==$cv['field']){
                        unset($creditdata[$k]);
                        continue;
                    }
                }
            }
            $cv['value']=$member['credit'.$cv['type']];
            if($cv['value']<=0){
                $cv['difee']=0.00;
                continue;
            }
            $cv['dirate']=$cv['dirate']?:100;
            $cv['rate']=$cv['rate']?:1;
            $cv['difee']=$pay['fee']*$cv['dirate']/100;
            $needfee=$cv['rate']*$cv['difee'];
            if($cv['difee']>0){
                if($needfee>$cv['value']){
                    $cv['difee']=$cv['value']/$cv['rate'];
                }
                $cv['difee']=price($cv['difee'],'int');
            }
        }
        $return['creditdata']=$creditdata;
    }
    if($pay['pay_type']){
        if(in_array($pay['pay_type'],$return['pay_types'])){
            $return['pay_type']=$pay['pay_type'];
        }else{
            if($pay['action']='member.recharge' && $setting['credit_wechat']){
                $return['pay_type']=$pay['pay_type'];
            }else{
                exi('没有可用的支付方式，请联系系统管理员！','error');
            }
        }
    }
    if(!$return['pay_types'] && !$return['pay_type']){
        exi('没有可用的支付方式，请联系系统管理员！','error');
    }
    if(count($return['pay_types'])==1 && (count($creditdata)==0 || $return['pay_types']['0']=='go2pay')){
        $return['pay_type']=$return['pay_types']['0'];
    }
    if($_RQ['forward_url']){
        $return['forward_url']=base64_decode($_RQ['forward_url']);
    }
    if($return['iswechat']){
        $return['type']='wechat';
    }elseif($return['iscredit']){
        $return['type']='credit';
    }
	if($_SESSION['container']=='aliapp'){
		$return['type']='alipay';
	}elseif($_SESSION['container']=='ttapp'){
		$return['type']='ttpay';
	}elseif($_SESSION['container']=='ksapp'){
		$return['type']='kspay';
	}
    if($return['isdiypay']){
        $return['diypay_title']=$setting['diypay_title'];
        $return['diypay_desc']=$setting['diypay_desc'];
    }
    if($return['isdiypay2']){
        $return['diypay2_title']=$setting['diypay2_title'];
        $return['diypay2_desc']=$setting['diypay2_desc'];
    }
	if($return['isgo2pay']){
        $return['go2pay_title']=$setting['go2pay_title'];
        $return['go2pay_desc']=$setting['go2pay_desc'];
    }
    if($pay['fee']<=0){
        $return['type']='credit';
    }
	if($setting['huifuapi_showtype']>=2 || $setting['huifuapi_showtype']){
        $return['pluginapi']='jy_huifuapi';
    }elseif($setting['icbcpay_showtype']>=2 || $setting['icbcpay_showtype']){
        $return['pluginapi']='jy_icbcpay';
    }elseif($setting['zwechat_showtype']>=2 || $setting['zwechat_showtype']){
        $return['pluginapi']='jy_zwechat';
    }elseif($setting['sandpay_showtype']>=2 || $setting['sandpay_showtype']){
        $return['pluginapi']='jy_sandpay';
    }elseif($setting['adapay_showtype']>=2 || $setting['adapay_showtype']){
        $return['pluginapi']='jy_adapay';
    }elseif($setting['allinpay_showtype']>=2 || $setting['allinpay_showtype']){
		$return['pluginapi']='jy_allinpay';
		$return['isallinpay']=2;
    }
    //余额抵扣
    if($pay['fee']>0 && $return['credit']>0 && $pay['fee']>$return['credit'] && !$setting['credit2_nodi']){
        $difee=$pay['fee']>$return['credit']?$return['credit']:$pay['fee'];
		if($setting['credit2_dirate']>0){
			$maxfee=$setting['credit2_dirate']*$pay['fee']/100;
			$maxfee=price($maxfee,'int');
			$difee=$maxfee>$difee?$difee:$maxfee;
		}
        $return['credit2']=array('isdi'=>1,'difee'=>$difee);
    }elseif($pay['fee']>0 && $return['credit']>0 && $setting['credit2_dirate']>0){
		$maxfee=$setting['credit2_dirate']*$pay['fee']/100;
		$maxfee=price($maxfee,'int');
        $difee=$maxfee>$return['credit']?$return['credit']:$maxfee;
        $return['credit2']=array('isdi'=>1,'difee'=>$difee);
    }
    $return['di_open']=0;
    $return['api']=$setting['api'];
    exi($return);
}
if($_RQ['op']=='alipay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'ordersn'=>$pay_log['tid'],
            'return_url'=>custom_url($pay_log['success_url'],true),
        );
        if($setting['huifuapi_showtype']>=2 || ($setting['huifuapi_showtype'] && $setting['alipay']!=1)){
            $pay_html=model('jy_huifuapi/pay')->create($params,'alipay');
        }elseif($setting['sandpay_showtype']>=2 || ($setting['sandpay_showtype'] && $setting['alipay']!=1)){
            $pay_html=model('jy_sandpay/pay')->create($params,'alipay');
        }elseif($setting['adapay_showtype']>=2 || ($setting['sandpay_showtype'] && $setting['alipay']!=1)){
            $pay_html=model('jy_adapay/pay')->create($params,'alipay');
        }elseif($setting['allinpay_showtype']>=2 || ($setting['allinpay_showtype'] && $setting['alipay']!=1)){
			$params['merchid']=$pay_log['merchid'];
			$params['paytype']='alipay';
			$pay_html=model('jy_allinpay/model')->unionorder($params);
         }else{
            $pay_html=model('jy_alipay/alipay')->mobile_pay($params);
        }
        exi(base64_encode($pay_html));
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='wechat'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $onemerchid=$_RQ['onemerchid'];
	$iswxapp=intval($_RQ['iswxapp']);
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
            'return_url'=>$pay_log['success_url'],
			'openid'=>$_SESSION['openid'],
        );
        if($_SESSION['container']=='wechat'){
            if($onemerchid){
                $jy_merch=cfc('core')->plugin_exists('jy_merch');
                if($jy_merch){
                    $onewechat=pdo_getcolumn('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$onemerchid),'onewechat');
                    $onewechat=json_decode($onewechat,true);
                    if($onewechat && $onewechat['isopen'] && $onewechat['appid'] && $onewechat['wechat_mchid']){
                        $config=array(
                            'appid'=>$onewechat['appid'],
                            'mch_id'=>$onewechat['wechat_mchid'],
                            'key'=>$onewechat['wechat_miyue'],
                            'apiclient_cert'=>$onewechat['cert_file']?ROOT_AT.$onewechat['cert_file']:'',
                            'apiclient_key'=>$onewechat['key_file']?ROOT_AT.$onewechat['key_file']:'',
                            'merchopenid'=>$_SESSION['openid'.$onemerchid],
                        );
                        $params['openid']=$_SESSION['openid'.$onemerchid];
                        $wechat_options=cfc('wechat_pay',$config)->jsapi_pay($params);
                        $isonemerchidpay=1;
                    }
                }
            }
            if($isonemerchidpay){
                pdo_update('core_paylog',array('onemerchid'=>$onemerchid),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
            }else{
                if($setting['huifuapi_showtype']>=2 || ($setting['huifuapi_showtype'] && $setting['wechat']!=1)){
                    $wechat_options=model('jy_huifuapi/pay')->create($params,'wechat');
                }elseif($setting['icbcpay_showtype']>=2 || ($setting['icbcpay_showtype'] && $setting['wechat']!=1)){
                    $pay_html=model('jy_icbcpay/pay')->create($params,'wechat');
					$wechat_options['payhtml']=base64_encode($pay_html);
					$wechat_options['ishtml']=1;
                }elseif($setting['zwechat_showtype']>=2 || ($setting['zwechat_showtype'] && $setting['wechat']!=1)){
                    $wechat_options['murl']=model('jy_zwechat/pay')->create($params,'wechat');
					$wechat_options['isurl']=1;
                }elseif($setting['sandpay_showtype']>=2 || ($setting['sandpay_showtype'] && $setting['wechat']!=1)){
                    $wechat_options['murl']=model('jy_sandpay/pay')->create($params,'wechat');
                    $wechat_options['isurl']=1;
                }elseif($setting['adapay_showtype']>=2 || ($setting['adapay_showtype'] && $setting['wechat']!=1)){
                    $wechat_options=model('jy_adapay/pay')->create($params,'wechat');
                }elseif($setting['allinpay_showtype']>=2 || ($setting['allinpay_showtype'] && $setting['wechat']!=1)){
					$params['iswxapp']=$iswxapp;
					$params['merchid']=$pay_log['merchid'];
					$params['paytype']='wechat';
					$wechat_options['murl']=model('jy_allinpay/model')->unionorder($params);
					$wechat_options['isurl']=1;
                }else{
                    if($setting['wechat']==1 || ($pay_log['action']='member.recharge' && $setting['credit_wechat'])){
                        $wechat=$setting;
                    }else{
                        //借用系统支付
                        exi('该公众号暂不支持微信支付！','error');
                        //获取粉丝openid
                    }
                    if(!$wechat['wechat_mchid'] || !$wechat['wechat_miyue']){
                        exi('支付出错：微信支付参数设置错误！','error');
                    }
                    $wechat_options=cfc('wechat_pay')->jsapi_pay($params);
                }
                pdo_update('core_paylog',array('onemerchid'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
            }
            exi($wechat_options);
        }else{
            //非微信环境，发起手机网页支付
            if($setting['huifuapi_showtype']>=2 || ($setting['huifuapi_showtype'] && $setting['wechat']!=1)){
                $pay_html=model('jy_huifuapi/pay')->create($params,'wechat');
				$murl['payhtml']=base64_encode($pay_html);
				$murl['ishtml']=1;
            }elseif($setting['icbcpay_showtype']>=2 || ($setting['icbcpay_showtype'] && $setting['wechat']!=1)){
                $pay_html=model('jy_icbcpay/pay')->create($params,'wechat');
				$murl['payhtml']=base64_encode($pay_html);
				$murl['ishtml']=1;
            }elseif($setting['zwechat_showtype']>=2 || ($setting['zwechat_showtype'] && $setting['wechat']!=1)){
                $murl['murl']=model('jy_zwechat/pay')->create($params,'wechat');
				$murl['isurl']=1;
            }elseif($setting['sandpay_showtype']>=2 || ($setting['sandpay_showtype'] && $setting['wechat']!=1)){
                $murl['murl']=model('jy_sandpay/pay')->create($params,'wechat');
				$murl['isurl']=1;
            }elseif($setting['allinpay_showtype']>=2 || ($setting['allinpay_showtype'] && $setting['wechat']!=1)){
				$params['iswxapp']=$iswxapp;
				$params['merchid']=$pay_log['merchid'];
				$murl['murl']=model('jy_allinpay/model')->unionorder($params);
				$murl['isurl']=1;
            }else{
                $murl=cfc('wechat_pay')->h5_pay($params);
            }
            exi($murl);
        }
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='wxapp_pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
	$onemerchid=$_RQ['onemerchid'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        if($params['fee']==0){
            exi('订单金额不能为零！','error');
        }
		if($onemerchid){
                $jy_merch=cfc('core')->plugin_exists('jy_merch');
                if($jy_merch){
                    $onewechat=pdo_getcolumn('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$onemerchid),'onewechat');
                    $onewechat=json_decode($onewechat,true);
                    if($onewechat && $onewechat['isopen'] && $onewechat['minappid'] && $onewechat['minmchid']){
                        $setting=array(
                            'appid'=>$onewechat['minsub_pay']?$onewechat['minmch_appid']:$onewechat['minappid'],
							'wxappid'=>$onewechat['minappid'],
							'mch_id'=>$onewechat['minmchid'],
							'sub_pay'=>$onewechat['minsub_pay'],
							'sub_mch_id'=>$onewechat['minsub_mch_id'],
							'sub_appid'=>$onewechat['minsub_appid'],
							'key'=>$onewechat['minpaykey'],
                            'merchopenid'=>$_SESSION['openid'.$onemerchid],
                        );
                        $params['openid']=$_SESSION['openid'.$onemerchid];
                        $res = cfc('wechat_pay', $setting)->wxapp_pay($params);
                        $isonemerchidpay=1;
                    }
                }
         }
		 if($isonemerchidpay){
                pdo_update('core_paylog',array('onemerchid'=>$onemerchid),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
         }else{
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']),array('appid','mch_appid','sub_pay','sub_mch_id','sub_appid','mchid','paykey'));
			if(!$wxapp || !$wxapp['appid'] || !$wxapp['mchid'] || !$wxapp['paykey']){
				exi('发起支付失败！小程序支付参数设置错误！','error');
			}
			if($setting['huifuapi_showtype']>=2 || ($setting['huifuapi_showtype'] && $setting['wechat']!=1)){
                $res=model('jy_huifuapi/pay')->create($params,'wechat',$wxapp['appid']);
            }elseif($setting['icbcpay_showtype']>=2 || ($setting['icbcpay_showtype'] && $setting['wechat']!=1)){
                $res=model('jy_icbcpay/pay')->create($params,'wechat',$wxapp['appid']);
            }elseif($setting['zwechat_showtype']>=2 || ($setting['zwechat_showtype'] && $setting['wechat']!=1)){
                $res=model('jy_zwechat/pay')->create($params,'wechat',$wxapp['appid']);
            }elseif($setting['sandpay_showtype']>=2 || ($setting['sandpay_showtype'] && $setting['wechat']!=1)){
				$res=model('jy_sandpay/pay')->create($params,'wechat',$wxapp['appid']);
			}elseif($setting['adapay_showtype']>=2 || ($setting['adapay_showtype'] && $setting['wechat']!=1)){
				$res=model('jy_adapay/pay')->create($params,'wechat',$wxapp['appid']);
			}elseif($setting['allinpay_showtype']>=2 || ($setting['allinpay_showtype'] && $setting['wechat']!=1)){
				$params['iswxapp']=1;
				$params['merchid']=$pay_log['merchid'];
				$params['paytype']='wechat';
				$res=model('jy_allinpay/model')->unionorder($params);
            }else{
				$setting=array(
					'appid'=>$wxapp['sub_pay']?$wxapp['mch_appid']:$wxapp['appid'],
					'wxappid'=>$wxapp['appid'],
					'mch_id'=>$wxapp['mchid'],
					'sub_pay'=>$wxapp['sub_pay'],
					'sub_mch_id'=>$wxapp['sub_mch_id'],
					'sub_appid'=>$wxapp['sub_appid'],
					'key'=>$wxapp['paykey'],
				);
				$res = cfc('wechat_pay', $setting)->wxapp_pay($params);
			}
			pdo_update('core_paylog',array('onemerchid'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
		 }
        if($_RQ['order_info']){
            //自定义组件获取支付参数
            $getpaymentparams=model($_RQ['order_info'].'/order')->getwx_shoppayment($pay_log['oid'],$pay_log['tid'],$_RQ['scene']);
            if($getpaymentparams['timeStamp']){
                $res=$getpaymentparams;
            }
        }
        exi($res);
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='aliapp_pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'ordersn'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        $wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id']?:$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']));
        $setting=array(
            'alipay_appid'=>$wxapp['ali_appid'],
            'alipay_private_key'=>$wxapp['alipay_private_key'],
            'alipay_public_key'=>$wxapp['alipay_public_key'],
        );
        $return['trade_no']=model('jy_alipay/alipay',$setting)->aliapp_pay($params);
        exi($return);
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='aliapp_pay_xinyong'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($pay_log['oldfee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'ordersn'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        $wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id']?:$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']));
		$wxapp['api_data']=json_decode($wxapp['api_data'],true);
		$params['ali_service_id']=$wxapp['api_data']['ali_service_id'];
		if(!$params['ali_service_id']){
			exi('小程序未绑定信用服务ID！','error');
		}
		$params['yafee']=$pay_log['oldfee'];
        $setting=array(
            'alipay_appid'=>$wxapp['ali_appid'],
            'alipay_private_key'=>$wxapp['alipay_private_key'],
            'alipay_public_key'=>$wxapp['alipay_public_key'],
            'target_app_id'=>$wxapp['target_app_id'],
        );
        $params['trade_no']=model('jy_alipay/alipay',$setting)->aliapp_pay_xinyong($params);
        exi($params);
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='aliapp_create_esign'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $params=array(
        'title'=>$pay_log['title'],
        'fee'=>$pay_log['fee'],
        'ordersn'=>$pay_log['tid'],
        'success_url'=>$pay_log['success_url'],
        'error_url'=>$pay_log['error_url'],
    );
    $wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id']?:$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']));
	$wxapp['api_data']=json_decode($wxapp['api_data'],true);
    $setting=array(
        'alipay_appid'=>$wxapp['ali_appid'],
        'alipay_private_key'=>$wxapp['alipay_private_key'],
		'alipay_public_key'=>$wxapp['alipay_public_key'],
        'target_app_id'=>$wxapp['target_app_id'],
    );
	$params['esigntpid']=$wxapp['api_data']['esigntpid'];
    $ret=model('jy_alipay/alipay',$setting)->ehtong_signflows_create($params);
    if($ret['code']==0){
        exi($ret['msg'],'error');
    }else{
		$return['isapi'] = $ret['isapi'];
		$return['shorturl'] = $ret['shorturl'];
		$return['url'] = $ret['url'];
        $return['flow_id'] = $ret['flow_id'];
        $return['file_url'] = $ret['file_url'];
        $return['file_name'] = $ret['file_name'];
    }
    exi($return);
}
if($_RQ['op']=='h5_create_esign'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $params=array(
        'title'=>$pay_log['title'],
        'fee'=>$pay_log['fee'],
        'ordersn'=>$pay_log['tid'],
        'success_url'=>$pay_log['success_url'],
        'error_url'=>$pay_log['error_url'],
    );
    $ret=model('jy_alipay/alipay',$setting)->ehtong_signflows_create($params);
    if($ret['code']==0){
        exi($ret['msg'],'error');
    }else{
        $return['flow_id'] = $ret['flow_id'];
        $params=array(
            'flow_id'=>$ret['flow_id'],
            'mid'=>$pay_log['mid'],
        );
        $ret=model('jy_alipay/alipay',$setting)->ehtong_signflows_url($params);
        if($ret['code']==0){
            exi($ret['msg'],'error');
        }else{
            $return['url']=$ret['sign_short_url'];
        }
    }
    exi($return);
}
//删除芝麻订单
if($_RQ['op']=='aliapp_del_xinyong'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    pdo_delete('jy_weishop_order',array('id'=>$pay_log['oid'],'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($pay_log['oldfee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'ordersn'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        $wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id']?:$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']));
		$wxapp['api_data']=json_decode($wxapp['api_data'],true);
		$params['ali_service_id']=$wxapp['api_data']['ali_service_id'];
		if(!$params['ali_service_id']){
			exi('小程序未绑定信用服务ID！','error');
		}
		$params['yafee']=$pay_log['oldfee'];
        $setting=array(
            'alipay_appid'=>$wxapp['ali_appid'],
            'alipay_private_key'=>$wxapp['alipay_private_key'],
            'alipay_public_key'=>$wxapp['alipay_public_key'],
            'target_app_id'=>$wxapp['target_app_id'],
        );
        $params['trade_no']=model('jy_alipay/alipay',$setting)->aliapp_cancel_xinyong($params);
	}
    exi('success');
}
if($_RQ['op']=='ttapp_pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        if($params['fee']==0){
            exi('订单金额不能为零！','error');
        }
        $wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']),array('tt_appid','tt_paymchid','tt_paysecret'));
        if(!$wxapp || !$wxapp['tt_appid'] || !$wxapp['tt_paymchid'] || !$wxapp['tt_paysecret']){
            exi('发起支付失败！头条小程序支付参数设置错误！','error');
        }
        $setting=array(
			'id'=>$_SESSION['appgid'],
            'app_id'=>$wxapp['tt_appid'],
            'tt_paymchid'=>$wxapp['tt_paymchid'],
            'tt_paysecret'=>$wxapp['tt_paysecret'],
        );
        $res=cfc('tt_pay',$setting)->ttapp_pay($params);
        exi($res);
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='ksapp_pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        if($params['fee']==0){
            exi('订单金额不能为零！','error');
        }
        $wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']),array('ks_appid','ks_secret','ks_paymchid','ks_paysecret'));
        if(!$wxapp || !$wxapp['ks_appid'] || !$wxapp['ks_secret']){
            exi('发起支付失败！小程序支付参数设置错误！','error');
        }
        $setting=array(
			'id'=>$_SESSION['appgid'],
            'app_id'=>$wxapp['ks_appid'],
			'ks_secret'=>$wxapp['ks_secret'],
            'ks_paymchid'=>$wxapp['ks_paymchid'],
            'ks_paysecret'=>$wxapp['ks_paysecret'],
        );
        $res=cfc('ks_pay',$setting)->ksapp_pay($params);
        exi($res);
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='credit'){
    $id=intval($_RQ['id']);
    $type=$_RQ['type'];
    if(!$id){
        exi('参数错误！','error');
    }
    $type=$type?:'credit';
    $creditfield=(!$type || $type=='credit')?'credit2':$type;
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$pay_log['realfee']=$pay_log['fee'];
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($pay_log['merchid']){
        $creditnum=pdo_getcolumn('jy_merch_member',array('merchid'=>$pay_log['merchid'],'mid'=>$_SESSION['uid']),$creditfield);
    }else{
        $creditnum=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),$creditfield);
    }
    $di_open=$_RQ['di_open'];
    $lastfee=$pay_log['fee'];
    //如果是余额支付，就不抵扣
    if($di_open && $di_open!='credit2'){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $lastfee=$lastfee-$difee;
    }
	$key=substr($creditfield, -1);
    $typetitle=credit_types()[$key];
    $credit_rate=$setting['credit'.$key.'_rate']?:1;
    if($lastfee*$credit_rate>$creditnum){
        exi($typetitle.'不足！当前账户['.$typetitle.']：'.$creditnum,'error');
    }
    $creditnum=$creditnum-($lastfee*$credit_rate);
    $update=array($creditfield=>$creditnum);
    //抵扣处理
    if($di_open && $difee>0){
        cfc('pay')->up_dipay($pay_log);
    }
    if($pay_log['merchid']){
        pdo_update('jy_merch_member',$update,array('merchid'=>$pay_log['merchid'],'mid'=>$_SESSION['uid']));
        $credittable='jy_merch_creditlog';
    }else{
        pdo_update('core_members',$update,array('id'=>$_SESSION['uid']));
        $credittable='core_credit_log';
    }
    pdo_update('core_paylog',array('pay_type'=>$type,'status'=>1,'paytime'=>time()),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    $log=array(
        'uniacid'=>$_SESSION['uniacid'],
        'mid'=>$_SESSION['uid'],
        'merchid'=>$pay_log['merchid'],
        'type'=>$key,
        'is_add'=>0,
        'fee'=>$lastfee,
        //'result'=>$credit2,
        'remark'=>'支付订单：'.$pay_log['title'],
        'createtime'=>TIMESTAMP
    );
    pdo_insert($credittable,$log);
    //此处模拟微信支付等进行异步支付结果通知~
    $data=array(
        'uniacid'=>$_SESSION['uniacid'],
        'onemerchid'=>$pay_log['merchid'],
        'uid'=>$_SESSION['uid'],
        'timestamp'=>TIMESTAMP,
        'back_url'=>$pay_log['back_url'],
        'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>$pay_log['di_credit'] && $lastfee<=0?$pay_log['di_credit']:$type,'fee'=>$pay_log['realfee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
    );
    $data['sign']=get_sign($data);
    $res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
    exi('success');
}
if($_RQ['op']=='hdfk'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$pay_log['realfee']=$pay_log['fee'];
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    pdo_update('core_paylog',array('pay_type'=>'hdfk','status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    //此处模拟微信支付等进行异步支付结果通知~
    $data=array(
        'uniacid'=>$_SESSION['uniacid'],
        'uid'=>$_SESSION['uid'],
        'timestamp'=>TIMESTAMP,
        'back_url'=>$pay_log['back_url'],
        'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>0,'pay_type'=>'hdfk','fee'=>$pay_log['realfee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
    );
    $data['sign']=get_sign($data);
    $res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
    exi('success');
}
if($_RQ['op']=='diypay'){
    if($_RQ['postdata']){
        $post=get_postdata();
        $id=intval($post['id']);
        $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$pay_log['realfee']=$pay_log['fee'];
		if($pay_log['jianfee']>0){
			$pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
		}
        $pay_type=$post['type']==2?'diypay2':'diypay';
        $update=array(
            'pay_type'=>$pay_type,
            'status'=>1,
            'diyfields'=>ijson_encode($post['diyfields']),
        );
        pdo_update('core_paylog',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//抵扣处理
		if($pay_log['di_credit'] && $pay_log['di_fee']>0){
			cfc('pay')->up_dipay($pay_log);
		}
        //此处模拟微信支付等进行异步支付结果通知~
        $data=array(
            'uniacid'=>$_SESSION['uniacid'],
            'uid'=>$_SESSION['uid'],
            'timestamp'=>TIMESTAMP,
            'back_url'=>$pay_log['back_url'],
            'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>$pay_type,'fee'=>$pay_log['realfee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
        );
        $data['sign']=get_sign($data);
        $res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
        exi('提交成功！','',$post['success_url']);
    }
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    $params=array(
        'id'=>$pay_log['id'],
        'title'=>$pay_log['title'],
        'diypay_title'=>$_RQ['type']==2?$setting['diypay2_title']:$setting['diypay_title'],
        'diypay_desc'=>$_RQ['type']==2?$setting['diypay2_title']:$setting['diypay_desc'],
        'diypay_code'=>$_RQ['type']==2?$setting['diypay2_code']:$setting['diypay_code'],
        'diypay_name'=>$_RQ['type']==2?$setting['diypay2_name']:$setting['diypay_name'],
        'diypay_number'=>$_RQ['type']==2?$setting['diypay2_number']:$setting['diypay_number'],
        'fee'=>$pay_log['fee'],
		'di_fee'=>$difee,
        'oldfee'=>$pay_log['oldfee'],
        'ordersn'=>$pay_log['tid'],
        'success_url'=>$pay_log['success_url'],
        'return_url'=>custom_url($pay_log['success_url'],true),
        'post'=>array(
            'id'=>$pay_log['id'],
            'type'=>$_RQ['type'],
            'diyfields'=>$_RQ['type']==2?$setting['diypay2_fileds']:$setting['diypay_fileds'],
            'success_url'=>$pay_log['success_url'],
        ),
    );
    exi($params);
}
if($_RQ['op']=='go2pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    $params=array(
        'id'=>$pay_log['id'],
        'title'=>$pay_log['title'],
        'go2pay_title'=>$setting['go2pay_title'],
        'go2pay_desc'=>$setting['go2pay_desc'],
        'fee'=>$pay_log['fee'],
		'di_fee'=>$difee,
        'oldfee'=>$pay_log['oldfee'],
        'ordersn'=>$pay_log['tid'],
        'success_url'=>$pay_log['success_url'],
        'return_url'=>custom_url($pay_log['success_url'],true),
        'post'=>array(
            'id'=>$pay_log['id'],
            'type'=>$_RQ['type'],
            'diyfields'=>$_RQ['type']==2?$setting['diypay2_fileds']:$setting['diypay_fileds'],
            'success_url'=>$pay_log['success_url'],
        ),
    );
    exi('','',$setting['go2pay_url']);
}
if($_RQ['op']=='yinlian'){

}
if($_RQ['op']=='allinpay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
	$iswxapp=intval($_RQ['iswxapp']);
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
			'iswxapp'=>$iswxapp,
			'tid'=>$pay_log['tid'],
			'merchid'=>$pay_log['merchid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
            'return_url'=>$pay_log['success_url'],
        );
        if($params['fee']==0){
            exi('订单金额不能为零！','error');
        }
		if($iswxapp){
			$params['paytype']='wechat';
		}
        $pay_html=model('jy_allinpay/model')->unionorder($params);
		if($iswxapp){
			exi($pay_html);
		}else{
			exi(base64_encode($pay_html));
		}
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='adapay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$pay_log['realfee']=$pay_log['fee'];
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
            'return_url'=>$pay_log['success_url'],
        );
        if($params['fee']==0){
            exi('订单金额不能为零！','error');
        }
        $type=$_RQ['type'];
		if($type=='fast_pay' && $setting['heepay_showtype']>=2){
			$pay_html=model('jy_heepay/pay')->create($params,$type);
		}else{
			if(!$setting['adapay_showtype'] || strpos($setting['adapay_pay_types'],'fast_pay')===false){
				$pay_html=model('jy_heepay/pay')->create($params,$type);
			}
			$pay_html=model('jy_adapay/pay')->create($params,$type);
		}
		if(!is_array($pay_html) || $pay_html['success']!=1){
			exi(base64_encode($pay_html));
		}else{
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'uid'=>$_SESSION['uid'],
				'timestamp'=>TIMESTAMP,
				'back_url'=>$pay_log['back_url'],
				'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>0,'pay_type'=>'fast_pay','fee'=>$pay_log['realfee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
			);
			$data['sign']=get_sign($data);
			$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
			exi('success');
		}
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='union_online' || $_RQ['op']=='alipay_url'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$pay_log['realfee']=$pay_log['fee'];
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'tid'=>$pay_log['tid'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
            'return_url'=>$pay_log['success_url'],
        );
        if($params['fee']==0){
            exi('订单金额不能为零！','error');
        }
        $type=$_RQ['type'];
        $paytype=$_RQ['op']=='alipay_url'?'alipay':'union_online';
        if($setting['sandpay_showtype']>=2){
            $pay_html=model('jy_sandpay/pay')->create($params,$paytype);
        }elseif($setting['adapay_showtype']>=2){
            $pay_html=model('jy_adapay/pay')->create($params,$paytype);
        }elseif($setting['allinpay_showtype']>=2){
			$params['merchid']=$pay_log['merchid'];
			$params['paytype']='union_online';
			$pay_html=model('jy_allinpay/model')->unionorder($params);
        }else{
            $pay_html=model('jy_alipay/alipay')->mobile_pay($params);
        }
        exi(base64_encode($pay_html));
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='hnapay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $pay_url=model('jy_hnapay/model')->pay_url($pay_log);
        exi(base64_encode($pay_url));
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='apipay'){
    $id=intval($_RQ['id']);
    $api_name=$_RQ['api_name'];
    if(!$id || !$api_name){
        exi('参数错误！'.$api_name,'error');
    }
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    $pay_log['oldfee']=$pay_log['fee'];
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id'],'uniacid'=>$_SESSION['uniacid']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'pay_id'=>$pay_log['id'],
            'success_url'=>$pay_log['success_url'],
            'error_url'=>$pay_log['error_url'],
        );
        $trade_no=model($api_name.'/pay')->gopay($params);
        if(!$trade_no){
            exi('fail');
        }
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
if($_RQ['op']=='vip8pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('参数错误！','error');
    }
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['jianfee']>0){
        $pay_log['fee']=$pay_log['fee']-$pay_log['jianfee'];
    }
    if($pay_log['fee']>0){
        if($setting['vip8pay']==1){
            $vip8pay=$setting;
        }else{
            //借用系统支付
            exi('该公众号暂不支持快捷支付！','error');
            //获取粉丝openid
        }
        if(!$vip8pay['vip8_shopid'] || !$vip8pay['vip8_uniacid'] || !$vip8pay['vip8_groupid']){
            exi('支付出错：快捷支付参数设置不全！','error');
        }
        $vip8_options=array(
            'shopid'=>$vip8pay['vip8_shopid'],
            'uniacid'=>$vip8pay['vip8_uniacid'],
            'groupid'=>$vip8pay['vip8_groupid'],
            'openid'=>$_SESSION['openid'],
            'mch_order_num'=>$pay_log['tid'],
            'type'=>2,
            'total_fee'=>$pay_log['fee'],
            'notify_url'=>SITEROOT.'/custom/payresult/vip8pay_notify.php',
            'source_id'=>0,
        );
        if($_SESSION['container']=='wechat'){
            $vip8_options['pay_type']='wechat_official_account';
        }else{
            //非微信环境，发起手机网页支付
            $vip8_options['pay_type']='wechat_official_account';
        }
        $res=cfc('pay')->vip8pay_build($vip8_options);
        exi($res);
    }else{
        pdo_update('core_paylog',array('pay_type'=>'wechat','status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
        //此处模拟微信支付等进行异步支付结果通知~
        $data=array(
            'uniacid'=>$_SESSION['uniacid'],
            'onemerchid'=>$pay_log['merchid'],
            'uid'=>$_SESSION['uid'],
            'merchid'=>$pay_log['merchid'],
            'timestamp'=>TIMESTAMP,
            'back_url'=>$pay_log['back_url'],
            'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
        );
        $data['sign']=get_sign($data);
        $res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
        exi('success');
    }

}
if($_RQ['op']=='wxapp_result'){
    $id=intval($_RQ['orderid']);
    $openid=trim($_RQ['openid']);
    if($_RQ['module_name'] == 'ewei_shopv2'){
        $_RQ['m']='ewei_shopv2';
        $_RQ['r']='app.order.pay.complete';
        $_RQ['do']='mobile';
        $_RQ['id']=$id;
        require(ROOT_D.'/api/open_jyapi/index.php');
        exit;
    }
}
